// TLogParse.cpp: implementation of the TLogParse class.
//
//////////////////////////////////////////////////////////////////////

#include "TLogParse.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TLogParse::TLogParse()
{
	iCall.eventTypeUid=KLogCallEventTypeUid;
	iSMS.eventTypeUid=KLogShortMessageEventTypeUid;
	today.HomeTime();
	todayDT=today.DateTime();
}

TLogParse::~TLogParse()
{

}

TBool TLogParse::IsInthisDay(const TTime& aLogTime)
{
	return	(todayDT.Day()==aLogTime.DateTime().Day());
}

TBool TLogParse::IsInthisWeek(const TTime& aLogTime)
{
	return (todayDT.Month()==aLogTime.DateTime().Month());
}

TBool TLogParse::IsInThisMonth(const TTime& aLogTime)
{
	return (today.WeekNoInYear()==aLogTime.WeekNoInYear());
}

void TLogParse::GetEventL(const CLogEvent &aEvent)
{
	TTime time=aEvent.Time();
	HBufC* direction = aEvent.Direction().AllocLC();
	TBuf<64> outgoing;
	outgoing.Append(_L("Outgoing"));
	TBuf<64> Incoming;
	Incoming.Append(_L("Incoming"));
	if(IsInThisMonth(time))
	{
		if(iCall.eventTypeUid==aEvent.EventType())
		{
			if (direction->Match(outgoing)!=KErrNotFound)
			{
				iCall.month.count_Out+=1;
				iCall.month.duration_Out+=aEvent.Duration();
			}
			if (direction->Match(Incoming)!=KErrNotFound)
			{
				iCall.month.count_In+=1;
				iCall.month.duration_In+=aEvent.Duration();
			}
		}
		if (iSMS.eventTypeUid==aEvent.EventType())
		{
			if (direction->Match(outgoing)!=KErrNotFound) 
				iSMS.month.count_Out+=1;
			if (direction->Match(Incoming)!=KErrNotFound)
				iSMS.month.count_Out+=1;
		}
		
		if (IsInthisWeek(time)) 
		{

			if(iCall.eventTypeUid==aEvent.EventType())
			{
				if (direction->Match(outgoing)!=KErrNotFound)
				{
					iCall.week.count_Out+=1;
					iCall.week.duration_Out+=aEvent.Duration();
				}
				if (direction->Match(Incoming)!=KErrNotFound)
				{
					iCall.week.count_In+=1;
					iCall.week.duration_In+=aEvent.Duration();
				}
			}
			if (iSMS.eventTypeUid==aEvent.EventType())
			{
				if (direction->Match(outgoing)!=KErrNotFound) 
					iSMS.week.count_Out+=1;
				if (direction->Match(Incoming)!=KErrNotFound)
					iSMS.week.count_Out+=1;
			}
			if (IsInthisDay(time)) 
			{
				if(iCall.eventTypeUid==aEvent.EventType())
				{
					if (direction->Match(outgoing)!=KErrNotFound)
					{
						iCall.day.count_Out+=1;
						iCall.day.duration_Out+=aEvent.Duration();
					}
					if (direction->Match(Incoming)!=KErrNotFound)
					{
						iCall.day.count_In+=1;
						iCall.day.duration_In+=aEvent.Duration();
					}
				}
				if (iSMS.eventTypeUid==aEvent.EventType())
				{
					if (direction->Match(outgoing)!=KErrNotFound) 
						iSMS.day.count_Out+=1;
					if (direction->Match(Incoming)!=KErrNotFound)
						iSMS.day.count_Out+=1;
				}
			}
		}
	}

	//delete HBufC
	CleanupStack::PopAndDestroy(direction);
}
